﻿// -----------------------------------------------------------------------
// <copyright file="ImageResult.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Website.CustomResults
{
	#region Usings

	using System.Web.Mvc;

	using MBOP.Core.Entities.Common;
	using MBOP.Website.ViewModels.Common;

	#endregion

	/// <summary>
	/// Encapsulate the image result of an action method and is used to perform a framework-level operation on behalf of the action method.
	/// </summary>
	public class ImageResult : ActionResult
	{
		#region Private Fields

		/// <summary>
		/// Represents image view model.
		/// </summary>
		private Image _model;

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ImageResult"/> class.
		/// </summary>
		/// <param name="model">The image view model</param>
		public ImageResult(Image model)
		{
			this._model = model;
		}

		#endregion

		#region Public Methods 

		/// <summary>
		/// Enables processing of the result of an action method.
		/// </summary>
		/// <param name="context">Controller context, which processing the current request.</param>
		public override void ExecuteResult(ControllerContext context)
		{
			if (this._model != null)
			{
				if (this._model.Uri != null)
				{
					context.HttpContext.Response.Redirect(this._model.Uri);
				}
				else if (this._model.Data != null && this._model.Data.Content != null && this._model.Data.ContentType != null )
				{
					context.HttpContext.Response.ContentType = this._model.Data.ContentType;
					context.HttpContext.Response.BinaryWrite(this._model.Data.Content);
				}
			}
		}

		#endregion
	}
}